package itesm.coler.service;
import java.sql.SQLException;

import itesm.coler.models.User;
import itesm.coler.converters.UserConverter;
import itesm.coler.cloudsql.*;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/api/info")
public class InfoResource {
	
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/usuario/{usuarioMail}/")
	public UserConverter getUsuario(@PathParam("usuarioMail") String mail) {
		ConnectDB mysql;
		try {
			mysql = new ConnectDB();
			UserDAO us = new UserDAO();
			us.setConnection(mysql.getConnectionDB());
			User ble = us.getUser(mail);
			UserConverter jiji= new UserConverter(ble);
			return jiji;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@PUT
	@Path("/user")
	@Consumes({"application/json"})
	public Response put(User user){
		try {
			ConnectDB cloudsql = new ConnectDB();
			UserDAO userdao = new UserDAO();
			userdao.setConnection(cloudsql.getConnectionDB());
			userdao.insertUser(user);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Response.ok().build();
	}
}